import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.Random;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;

import java.awt.Font;

import javax.swing.JLabel;

import java.awt.Component;

import javax.swing.Box;

import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JButton;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class QuickFrame extends JFrame {

	private JPanel contentPane;
	private Vector<Integer> unsorted = new Vector<Integer>();
	private JTextArea textArea_1 = new JTextArea();
	private JTextArea textArea = new JTextArea();
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					QuickFrame frame = new QuickFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public QuickFrame() {
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{0, 0, 0};
		gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
		gbl_contentPane.columnWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{0.0, 1.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
		
		JLabel lblNewLabel = new JLabel("Numbers before sort");
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.weighty = 1.0;
		gbc_lblNewLabel.weightx = 1.0;
		gbc_lblNewLabel.gridwidth = 1;
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
		gbc_lblNewLabel.gridx = 0;
		gbc_lblNewLabel.gridy = 0;
		contentPane.add(lblNewLabel, gbc_lblNewLabel);
		
		textArea.setFont(new Font("Dialog", Font.PLAIN, 15));
		textArea.setEditable(false);
		textArea.setLineWrap(true);
		textArea.setOpaque(false);
		GridBagConstraints gbc_textArea = new GridBagConstraints();
		gbc_textArea.weighty = 1.0;
		gbc_textArea.weightx = 1.0;
		gbc_textArea.gridwidth = 1;
		gbc_textArea.insets = new Insets(0, 0, 5, 0);
		gbc_textArea.fill = GridBagConstraints.BOTH;
		gbc_textArea.gridx = 0;
		gbc_textArea.gridy = 1;
		
		Random r = new Random();
		
		String display = new String();
		
		for(int i = 0; i < 25; i++){
			unsorted.add(r.nextInt(1000));
			display = display.concat(unsorted.get(i)+"");
				if(i != 24)
					display = display.concat(", ");
			}	
		
		textArea.setText(display);
		contentPane.add(textArea, gbc_textArea);
		
		textArea_1.setFont(new Font("Dialog", Font.PLAIN, 15));
		textArea_1.setLineWrap(true);
		textArea_1.setEditable(false);
		textArea_1.setOpaque(false);
		GridBagConstraints gbc_textArea_1 = new GridBagConstraints();
		gbc_textArea_1.weighty = 1.0;
		gbc_textArea_1.weightx = 1.0;
		gbc_textArea_1.gridwidth = 1;
		gbc_textArea_1.insets = new Insets(0, 0, 5, 0);
		gbc_textArea_1.fill = GridBagConstraints.BOTH;
		gbc_textArea_1.gridx = 0;
		gbc_textArea_1.gridy = 2;
		contentPane.add(textArea_1, gbc_textArea_1);
		
		JButton btnNewButton = new JButton("Sort");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Vector<Integer> sorted = quicksort(unsorted);
				
				String display = new String();
				
				for(int i = 0; i < 25; i++){
					display = display.concat(sorted.get(i)+"");
					
						if(i != sorted.size()-1)
							display = display.concat(", ");
					}		
				textArea_1.setText(display);
			}
		});
		GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
		gbc_btnNewButton.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnNewButton.weighty = 0.25;
		gbc_btnNewButton.weightx = 1.0;
		gbc_btnNewButton.gridx = 0;
		gbc_btnNewButton.gridy = 3;
		contentPane.add(btnNewButton, gbc_btnNewButton);
		
		JButton btnNewButton_1 = new JButton("Randomize");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Random r = new Random();
				
				String display = new String();
				unsorted.clear();
				
				for(int i = 0; i < 25; i++){
					unsorted.add(r.nextInt(1000));
					display = display.concat(unsorted.get(i)+"");
						if(i != 24)
							display = display.concat(", ");
					}		
				textArea.setText(display);
				textArea_1.setText("");
				contentPane.repaint();
			}
		});
		GridBagConstraints gbc_btnNewButton_1 = new GridBagConstraints();
		gbc_btnNewButton_1.weighty = 0.25;
		gbc_btnNewButton_1.weightx = 1.0;
		gbc_btnNewButton_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnNewButton_1.gridx = 0;
		gbc_btnNewButton_1.gridy = 4;
		contentPane.add(btnNewButton_1, gbc_btnNewButton_1);
		
	}
	
	private Vector<Integer> quicksort(Vector<Integer> list)
	{	
		if(list.size()<= 1){
			return list;}
		
		int pivot = list.size()-1;
		int pivot_val = list.get(pivot);
		
		list.remove(pivot);
		
		Vector<Integer> left = new Vector<Integer>();
		Vector<Integer> right = new Vector<Integer>();
		
		for(int i = 0; i < list.size(); i++){
			if(list.get(i) < pivot_val)
				left.add(list.get(i));
			else
				right.add(list.get(i));
		}
		
		Vector<Integer> sorted = new Vector<Integer>();
		sorted.addAll(quicksort(left));
		sorted.add(pivot_val);
		sorted.addAll(quicksort(right));
		
		return sorted;
	}
	
}
